package com.erpoa.controller;

import com.alibaba.fastjson.JSON;
import com.erpoa.annotation.NoRepeatSubmit;
import com.erpoa.dto.DealMessage;
import com.erpoa.entity.PackMessage;
import com.erpoa.service.GoodRetMaterialService;
import com.erpoa.utils.PDAException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.CannotSerializeTransactionException;
import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.web.bind.annotation.*;

/**
 * @author chenjimin
 * @Title: GoodRetMaterialController
 * @ProjectName oaerp
 * @Description: 批次退料
 * @date 2020/9/2213:03
 */
@CrossOrigin(maxAge = 3600)
@Api(value = "GoodRetMaterialController", tags = {"批次退料"})
@RestController
@RequestMapping("/PDAService")
public class GoodRetMaterialController {
    private static final Logger logger = LoggerFactory.getLogger(GoodsOutMaterialController.class);
    @Autowired
    private GoodRetMaterialService goodRetMaterialService;

    @ApiOperation("批次退料")
    @RequestMapping(value = {"/goodretmaterialservlet"}, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @NoRepeatSubmit(key = "goodretmaterialservlet:arg[0]")
    public DealMessage GoodRetMaterialServlet(@RequestBody PackMessage packMessage) {
        DealMessage dealMessage = new DealMessage(true);
        int optType = packMessage.getOptType();
        Integer posValue = packMessage.getPos();
        String hrno = packMessage.getUser().getsCode();
        dealMessage.setPos(posValue);
        try {
            logger.debug("线程:" + Thread.currentThread().getId() + "请求开始：" + packMessage.toString());
            System.out.println("线程:" + Thread.currentThread().getId() + "请求开始：" + packMessage.toString());
            switch (optType) {
                case 1:
                    dealMessage.setMessage(JSON.toJSONString(goodRetMaterialService.functionGetOrderPlan(packMessage)));
                    break;
                case 2:
                    dealMessage.setMessage(JSON.toJSONString(goodRetMaterialService.functionSubRecMat(packMessage)));
                    break;
                default:
                    ;
                    dealMessage.setMessage(JSON.toJSONString(goodRetMaterialService.functionGetQty(packMessage)));
                    break;

            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof PDAException) {
                dealMessage = new DealMessage(false, e.getMessage());
            } else if (e instanceof CannotSerializeTransactionException || e instanceof CannotCreateTransactionException) {

                dealMessage = new DealMessage(false, "数据正在处理中，请再试一次！");

            } else if (e instanceof TooManyResultsException) {
                dealMessage = new DealMessage(false, "数据错误，请检查！" + e.getMessage());

            } else {
                dealMessage = new DealMessage(false, "服务器hj接口出现未知错误，请再扫一次确认结果，如多次出现请及时反馈IT，分机号：6257！时间点：" + System.currentTimeMillis() + "错误信息：" + e.getMessage());
            }
            logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
            System.out.println("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
            return dealMessage;
        }
        logger.debug(dealMessage.toString());
        logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
        System.out.println("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
        return dealMessage;
    }

}
